package com.mini.gateway.web.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * CommonController
 *
 */
@Slf4j
@Controller
@RequestMapping("/gateway")
public class CommonController {

    @Autowired
    private ConfigurableEnvironment environment;

    @GetMapping("/")
    public String toPage() {
        return "common/main";
    }

    @GetMapping("/main")
    public String toMainPage() {
        /*MutablePropertySources propertySources = environment.getPropertySources();
        System.out.println(propertySources);
        int mils = 10000;
        PropertySource prop = new PropertySource("hystrix.command.tenbent-task.execution.isolation.thread.timeoutInMilliseconds", mils) {
            @Override
            public Object getProperty(String name) {
                return "test";
            }
        };
        propertySources.addLast(prop);*/
        return "common/main";
    }

    @GetMapping("/welcome")
    public String toWelcomePage() {

        return "common/welcome";
    }

    @GetMapping(value = "/fallback")
    @ResponseBody
    public ResponseEntity fallBackController() {
        Map<String, String> res = new HashMap(8);
        res.put("code", "9988");
        res.put("data", "Request time out");
        // 此处需要设置成为 GATEWAY_TIMEOUT, 如果设置为 REQUEST_TIMEOUT 的话浏览器发起会重试
        return ResponseEntity.status(HttpStatus.GATEWAY_TIMEOUT).body(res);
    }
}
